<template>
  <a-modal
    title="编辑装配工单"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
            <a-form-item v-show="true" label="订单编号" :labelCol="labelCol_T" :wrapperCol="wrapperCol_T" has-feedback>
              <a-input readOnly placeholder="订单编号无需输入" v-decorator.trim="['code']" />
            </a-form-item>
          </a-col>
        </a-row>
        <ass-table-form-item
          :dataSource="workOrderList"
          :proList="list.proList"
          :plaNum="form.getFieldValue('plaNum') ? form.getFieldValue('plaNum') : null"
          @add="
            ({ tableData }) => {
              workOrderList = tableData
            }
          "
          @del="
            ({ tableData }) => {
              workOrderList = tableData
            }
          "
        />
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入备注" v-decorator="['remarks']" />
        </a-form-item>
      </a-form></a-spin>
  </a-modal>
</template>

<script>
  import { assEdit } from '@/api/modular/main/ass/assManage'
  import assTableFormItem from '@/views/main/ass/assTableFormItem'
  import { proList } from '@/api/modular/main/pro/proManage'
  import XEUtils from 'xe-utils'
  export default {
    components: { assTableFormItem },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        labelCol_T: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol_T: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        list: {
          // 产品
          proList: []
        },
        workOrderList: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    provide() {
      return {
        getProList: this.getProList
      }
    },
    methods: {
      // 初始化方法
      async edit(record) {
        this.visible = true
        this.workOrderList = record.workOrderList
        await this.getProList()
        setTimeout(() => {
          this.form.setFieldsValue({
            // 表单接收数据
            code: record.code,
            // 7.14日v-decorator报错
            id: record.id,
            // proId: record.proId,
            // plaNum: record.plaNum,
            remarks: record.remarks
          })
        }, 100)
      },
      // 获取proList表单
      async getProList() {
        this.list.proList = []
        var promise = proList().then((res) => {
          this.list.proList = res.data
        })
        await promise
        return this.list.proList
      },
      handleSubmit() {
        const {
          form: { validateFields }
        } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.code = XEUtils.trim(values.code)
            for (const key in values) {
              if (typeof values[key] === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.workOrderList = XEUtils.orderBy(this.workOrderList, [['plaNum', 'asc']]) || null
            values.code = values.code.trim()
            assEdit(values)
              .then((res) => {
                if (res.success) {
                  this.$message.success('编辑成功')
                  this.confirmLoading = false
                  this.$emit('ok', values)
                  this.handleCancel()
                } else {
                  this.$message.error(res.message) //  + res.message
                }
              })
              .finally((res) => {
                this.confirmLoading = false
              })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
        this.workOrderList = []
        this.$emit('ok')
      }
    }
  }
</script>
